import hudson.model.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

def setKeyValue(key, value, file_path) {
    Properties prop = new Properties()
    try {
        prop.load(new FileInputStream(file_path))
    } catch (FileNotFoundException e) {
        e.printStackTrace()
    } catch (IOException e) {
        e.printStackTrace()
    }

    try {
        prop.setProperty(key, value)
        FileOutputStream fos = new FileOutputStream(file_path)
        prop.store(fos, null)
        fos.close()
    } catch (FileNotFoundException e) {
        e.printStackTrace()
    } catch (IOException e) {
        e.printStackTrace()
    }
}

def setKeyValue2(key, value, file_path) {
    file_content_old = readFile file_path
    println file_content_old
    lines = file_content_old.tokenize("\n")
    new_lines = []
    lines.each { line -> 
        if (line.trim().startsWith(key)) {
            line = key + "=" + value
            new_lines.add(line)
        } else {
            new_lines.add(line)
        }
    }
    file_content_new = ""
    new_lines.each{line ->
        file_content_new += line + "\n"
    }
    writeFile file: file_path, text: file_content_new, encoding: "UTF-8"
}
return this